﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace r2mp_GUI
{

    /// <summary>
    /// This class provides functionality to save the contents of a panel into a file as a bitmap.
    /// </summary>
    class SaveDiagram
    {

        /// <summary>
        /// Draws a control into a bitmap
        /// </summary>
        /// <param name="control">The control to draw</param>
        /// <param name="bitmap">The bitmap to draw into</param>
        private static void DrawControl(Control control, Bitmap bitmap)
        {
            control.DrawToBitmap(bitmap, control.Bounds);
            foreach (Control childControl in control.Controls)
            {
                DrawControl(childControl, bitmap);
            }
        }

        /// <summary>
        /// Takes care of creating a bitmap and storing a panel into it, also shows a savefiledialog
        /// </summary>
        /// <param name="p">The panel to save</param>
        public static void SaveBitmap(Panel p)
        {
            Bitmap bmp = new Bitmap(p.Width, p.Height);

            p.DrawToBitmap(bmp, new Rectangle(0, 0, p.Width, p.Height));
            foreach (Control control in p.Controls)
            {
                DrawControl(control, bmp);
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Bitmap |*.bmp";
            sfd.DefaultExt = ".bmp";
            if (sfd.ShowDialog() == DialogResult.OK)
                bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);            
        }
    }
}
